package com.project.im.sdk.service;

import cn.hutool.core.codec.Base64;
import com.project.im.app.request.AuthRequest;
import com.project.im.app.response.AuthResponse;
import com.project.im.common.util.DHUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
public class AuthService {


    public AuthResponse auth(AuthRequest authRequest) {
        // 校验token是否合法

        // 生成密钥对
        Map<String, Object> keyPair = DHUtils.initKey();

        // 根据公钥获取加密密钥
        String secretKey = DHUtils.getSecretKey(authRequest.getPublicKey(), DHUtils.getPrivateKey(keyPair));
        log.info("【生成秘钥】secretKey: {}", secretKey);

        // 返回公钥
        String serverPublicKey = Base64.encode(DHUtils.getPublicKey(keyPair).getY().toByteArray());
        return new AuthResponse().setPublicKey(serverPublicKey);
    }
}
